package cn.com.fattiger.framework.cms.admin.common.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.fattiger.framework.cms.admin.common.model.Answer;
import cn.com.fattiger.framework.cms.admin.common.model.Option;
import cn.com.fattiger.framework.cms.admin.common.model.Question;
import cn.com.fattiger.framework.cms.admin.common.model.Questionnaire;
import cn.com.fattiger.framework.cms.admin.common.service.AnswerService;
import cn.com.fattiger.framework.cms.admin.common.service.OptionService;
import cn.com.fattiger.framework.cms.admin.common.service.QuestionService;
import cn.com.fattiger.framework.cms.admin.common.service.QuestionnaireService;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("question")
public class WJQuestionController {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private QuestionnaireService questionnaireService;
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private OptionService optionService;
	
	@Autowired
	private AnswerService answerService;

	@RequestMapping("list.do")
	public String getList(Integer page, Integer pageSize, Long questionnaireId, ModelMap map){
		Questionnaire questionnaire = questionnaireService.get(questionnaireId);
		map.put("questionnaire", questionnaire);
		if(page == null || page < 0){
			page = 0;
		}
		if(pageSize == null || pageSize < 0 || pageSize > 100){
			pageSize = 30;
		}
		Map<String,String> condition = new HashMap<String, String>();
		condition.put("questionnaireId", questionnaireId.toString());
		Page<Question> pageList = questionService.list(page, pageSize, condition);
		map.put("lists", 		pageList.getContent());
    	map.put("totalPageNum", pageList.getTotalPages());
    	map.put("currentPage", 	pageList.getNumber());
    	map.put("totalNum", 	pageList.getTotalElements());
		return "questionnaire/question/question_list";
	}
	
	@RequestMapping(value = "edit.do", method = RequestMethod.GET)
	public String edit(Long id, Long questionnaireId, Integer page, ModelMap map){
		Question entity = null;
		if(id != null){
			entity = questionService.get(id);
			String json = null;
			try {
				ObjectMapper mapper = new ObjectMapper();
				json = mapper.writeValueAsString(entity);
			} catch (JsonProcessingException e) {
				logger.error(e.getMessage(),e);
			}
			logger.info(json);
		}else{
			map.put("questionnaireId", questionnaireId);
		}
		map.put("entity", entity);
		map.put("page", page);
		return "questionnaire/question/question_edit";
	}
	
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> save(@RequestBody Question entity){
 		try {
 			ObjectMapper mapper = new ObjectMapper();
 			logger.info(mapper.writeValueAsString(entity));
 		} catch (JsonProcessingException e) {
 			logger.error(e.getMessage(),e);
 		}
		questionService.save(entity);
 		Map<String, String> result = new HashMap<String, String>();
		result.put("result", "success");
		return result;
	}
	
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> update(@RequestBody Question entity){
 		try {
 			ObjectMapper mapper = new ObjectMapper();
 			logger.info(mapper.writeValueAsString(entity));
 		} catch (JsonProcessingException e) {
 			logger.error(e.getMessage(),e);
 		}
		questionService.update(entity);
 		Map<String, String> result = new HashMap<String, String>();
		result.put("result", "success");
		return result;
	}
	
	
	@RequestMapping(value="del.do", method = RequestMethod.GET)
	public String del(Long id, Integer page, Long questionnaireId){
		questionService.delete(id);
		return "redirect:list.do?page=" + page + "&questionnaireId=" + questionnaireId;
	}
	
	
	@RequestMapping(value = "delOption.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> delOption(Long id){
		optionService.delete(id);
		Map<String, String> result = new HashMap<String, String>();
		result.put("result", "success");
		return result;
	}
	
	@RequestMapping(value = "updateOption.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> updateOption(@RequestBody Option option){
		try {
 			ObjectMapper mapper = new ObjectMapper();
 			logger.info(mapper.writeValueAsString(option));
 		} catch (JsonProcessingException e) {
 			logger.error(e.getMessage(),e);
 		}
		if(option.getId() != null){
			Option entity = optionService.get(option.getId());
			entity.setContent(option.getContent());
			entity.setSequence(option.getSequence());
			optionService.save(entity);
		}else{
			optionService.save(option);
		}
		Map<String, String> result = new HashMap<String, String>();
		result.put("result", "success");
		return result;
	}
	
	@RequestMapping(value = "getOptionCount.do")
	@ResponseBody
	public Map<String,Object> getOptionCount(Long id){
		logger.info(id);
		Map<String, Object> result = new HashMap<String, Object>();
		List<Answer> list = answerService.getOptionCount(id);
		try {
 			ObjectMapper mapper = new ObjectMapper();
 			logger.info(mapper.writeValueAsString(list));
 		} catch (JsonProcessingException e) {
 			logger.error(e.getMessage(),e);
 		}
		result.put("data", list);
		return result;
	}
	
}
